<?php
App::uses('AppController', 'Controller');
class TopicsController extends AppController {
var $name = "Topic";// ten cua Controller
    var $helpers = array('Paginator','Html');
    var $paginate = array();
	public function beforeFilter() {
		parent::getTopic();
		parent::beforeFilter();
		
	}
	public function index() {
		$this->set("strTitle","Chủ đề");
		$conditions = array("Topic.deleted" => 0);
		$this->paginate = array(
                                'limit' => ITEM_PER_PAGE,
                                'order' => array('id' => 'asc'),
                             );
        $data = $this->paginate("Topic",$conditions);
        $this->set("data",$data);
		// echo "<pre>";
		// print_r($data);exit();
	}
	public function add(){
		$this->set("strTitle","Thêm Chủ đề");
		if ($this -> request -> is('post')) {
			$new = $this->data;
			$new['deleted'] = 0;
			if ($this -> Topic -> save($new)){
				$this->Session->setFlash('Thêm thành công');
				$this->redirect(array('action' => 'index'));
			}
		}
		
	}
	public function edit($id){
		$this->set("strTitle","Chỉnh sửa Chủ đề");
		$conditions = array("Topic.id" => $id);
		$data = $this->Topic->find('first', array('conditions' => $conditions));
		$this -> set("data", $data);
		if ($this -> request -> is('post')) {
			$new = $this->data;
			$new['deleted'] = 0;
			if ($this -> Topic -> save($new)){
				$this->Session->setFlash('edit thành công');
				$this->redirect(array('action' => 'index'));
			}
		}
		
	}
	public function delete($id){
		$conditions = array("Topic.id" => $id);
		$data = $this->Topic->find('first', array('conditions' => $conditions));
		$new = $data['Topic'];
		$new['deleted'] = 1;
		if ($this -> Topic -> save($new)){
			$this->Session->setFlash('Thêm thành công');
			$this->redirect(array('action' => 'index'));
		}
	}
}